package ch3stacks;

import java.util.Stack;

class Tower {
	private Stack<Integer> disks;
	private int index;

	public Tower(int i) {
		disks = new Stack<Integer>();
		index = i;
	}

	public int index() {
		return index;
	}

	public void print() {
		System.out.println("Contents of Tower " + index());
		for (int i = disks.size() - 1; i >= 0; i--) {
			System.out.println(" " + disks.get(i));
		}
	}

	public void add(int d) {
		for (int i : disks) {
			if (i <= d) {
				throw new RuntimeException();
			}
		}
		disks.push(d);
	}

	public void moveDisks(int n, Tower destination, Tower buffer) {
		if (n <= 0) {
			return;
		}
		
		moveDisks(n-1, buffer, destination);
		Integer pop = disks.pop();
		destination.add(pop);
		System.out.println("moving disk "+pop+" from "+index+" to "+destination.index);
		buffer.moveDisks(n-1, destination, this);
	}
}

public class Ex4 {

	public static void main(String... argv) {
		int n = 3;
		Tower[] towers = new Tower[n];
		for (int i = 0; i < 3; i++)
			towers[i] = new Tower(i);
		for (int i = n - 1; i >= 0; i--)
			towers[0].add(i);
		towers[0].moveDisks(n, towers[2], towers[1]);
	}

}
